from django.shortcuts import render
from rest_framework import viewsets

# Create your views here.
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.status import HTTP_404_NOT_FOUND, HTTP_204_NO_CONTENT

from drfstudy.models import Student, Classes
from drfstudy.serializers import StudentSerializer, ClassSerializer


# class StudentViewSet(viewsets.ViewSet):
#     # 提供所有的学生数据
#     def list(self, request):
#         # 通过模型查询到所有的学生数据
#         # 显示的是没有被逻辑删除的学生数据
#         students = Student.objects.filter(is_delete=False)
#         # 通过序列化器进行转换 ，做序列化的操作，把查询集转为json格式的数据
#         # 实例化序列化器，传入要转换的数据,many=True表示传入的是多条数据
#         # serializer 保存的应该是一个对象
#         serializer = StudentSerializer(students, many=True)
#         print(serializer.data, type(serializer.data))
#         return Response(serializer.data)
#
#     def create(self, request):
#         # 获取客户端传入的数据，进行反序列化 用户提交的数据
#         serializer = StudentSerializer(data=request.data)
#         # 通过序列化器对数据进行校验 校验不通过则发送异常
#         serializer.is_valid(raise_exception=True)
#         # 保存 持久化
#         serializer.save()  # create方法
#         return Response(serializer.data)  # 把创建出来的数据进行返回
#
#     # students/1
#     def retrieve(self, request, pk):
#         # pk  指定数据id
#         try:
#             student = Student.objects.filter(is_delete=False).get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#         # 序列化的操作 把模型对象转json格式的数据
#         serializer = StudentSerializer(student)
#         return Response(serializer.data)
#
#     # students/1
#     def update(self, request, pk):
#         try:
#             student = Student.objects.get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#         # 进行反序列化的操作
#         serializer = StudentSerializer(student, data=request.data)
#         # 校验数据
#         serializer.is_valid()
#         # 保存数据
#         serializer.save()  # update
#         return Response(serializer.data)
#
#     def destory(self, request, pk):
#         try:
#             student = Student.objects.get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#         # 调用了删除的方法 但是实际上我是做的修改的操作
#         # 把逻辑删除的值修改True
#         student.delete()
#         return Response(status=HTTP_204_NO_CONTENT)
# ModelViewSet已经帮我们封装好了五个增删改查的方法，所以就不需要额外自定义方法了
class StudentViewSet(viewsets.ModelViewSet):
    # 指定数据查询集
    queryset = Student.objects.filter(is_delete=False)
    # 指定序列化器类
    serializer_class = StudentSerializer


class ClassViewSet(viewsets.ModelViewSet):
    # 班级表里面的所有数据
    queryset = Classes.objects.filter(is_delete=False)
    serializer_class = ClassSerializer

    # 需求 查询最新的班级
    # api设计 ：请求方式 GET /classes/last/
    # 默认就会以方法名为路由添加到后面  detail是否要传id
    @action(methods=['get'], detail=False)
    def last(self, request):
        # 获取班级数据的最后一条数据
        classes = Classes.objects.last()
        # 把后端的数据序列化
        # ClassSerializer(classes) 等同于下面的写法
        data = self.get_serializer(classes).data
        # 获取的最后一个班级信息返回
        return Response(data)

    # 获取指定班级的最后一个学生
    # api设计 请求方式['get']
    # class/pk/last_student
    # r取消/+字母变成转义符
    # url_path=r'student/last' 可写可不写 class/pk/student/last/
    @action(methods=['get'], detail=True, url_path=r'student/last')
    def last_student(self, request, pk):
        # 得到指定的班级信息
        try:
            classes = Classes.objects.get(id=pk)
        except Classes.DoesNotExist:
            return Response(status=HTTP_404_NOT_FOUND)
        # 得到最后一个学生信息
        class_student_last = classes.student_set.last()
        # 嵌套
        data = StudentSerializer(class_student_last).data
        return Response(data)
